package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingController {
    @Autowired
    private ShoppingCartService cartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：[]",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或者套餐是否在购物车中
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();
        if (dishId!=null){
            //添加的种类是菜品
            //用户ID和菜品ID联合添加指定菜品
            lqw.eq(ShoppingCart::getDishId,dishId);

        }else{
            //添加的种类是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
//SQL:select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart sc = cartService.getOne(lqw);


        //如果已存在并且口味一样，数量加一 &&shoppingCart.getDishFlavor().equals(sc.getDishFlavor())
        if (sc!=null){
            //不执行插入操作，数量加一即可
            Integer number = sc.getNumber();
            sc.setNumber(number +1);
            cartService.updateById(sc);
        }else {
            //不存在或口味不一样，执行添加操作,第一次入库number不存在（查看log信息可知），手动数量默认1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            cartService.save(shoppingCart);
            //保存完之后ID赋值了，覆盖自己获取的购物车信息
            sc=shoppingCart;
        }


        return R.success(sc);
    }
    //购物车减去路径是sub，收到参数查一下数据库数量，大于1就减1，等于1就删掉

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //前端传过来的是菜品ID或者套餐ID
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId()!=null){
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart sc = cartService.getOne(lqw);
        Integer number = sc.getNumber();
        if (number>1){
            sc.setNumber(number-1);
            cartService.updateById(sc);
        }
        else {
            cartService.removeById(shoppingCart.getId());
        }

        return R.success(sc);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = cartService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        //SQL:delete from shopping_cart where user_id = ?

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        cartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }
}

